/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.activity.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.activity.constant.CardConstant;
import com.ykht.proj.activity.entity.*;
import com.ykht.proj.activity.mapper.VGroupMapper;
import com.ykht.proj.activity.mapper.VOrderCardShopMapper;
import com.ykht.proj.activity.service.VOrderCardShopService;
import com.ykht.proj.activity.vo.CardShopDto;
import com.ykht.proj.common.core.util.R;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 购买卡和商铺关联表
 *
 * @author csg
 * @date 2021-05-21 14:22:31
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class VOrderCardShopServiceImpl extends ServiceImpl<VOrderCardShopMapper, VOrderCardShop> implements VOrderCardShopService {

	private final VOrderCardShopMapper vOrderCardShopMapper;

	/**
	 * 获取 试用门店列表（提取）
	 *
	 * @param cardShopDto    条件
	 * @return
	 */
	@Override
	public List<CardShopDto> getCardShopPage(CardShopDto cardShopDto) {
		List<CardShopDto> cardShopDtoList = vOrderCardShopMapper.getCardShopPage(cardShopDto);
		log.info("->根据优惠方案ID给数据赋值优惠价格");
		if (null != cardShopDtoList && cardShopDtoList.size() > 0){
			List<Long> discountPlanIdList = cardShopDtoList.stream().map(CardShopDto::getDiscountPlanId).collect(Collectors.toList());
			List<VDiscountPlan> discountPlanList = new VDiscountPlan().selectList(
					Wrappers.lambdaQuery(VDiscountPlan.class).in(VDiscountPlan::getId, discountPlanIdList));
			cardShopDtoList.stream().forEach(itme -> {
				discountPlanList.stream().forEach(plan ->{
					if (plan.getId().equals(itme.getDiscountPlanId())){
						itme.setDiscount(plan.getDiscount());
						itme.setLowMoney(plan.getLowMoney());
						itme.setPlanType(plan.getPlanType());
					}
				});
			});
		}
		return cardShopDtoList;
	}

	/**
	 * PC 获取会员能使用门店列表 分页
	 *
	 * @param page
	 * @param cardShopDto
	 * @return
	 */
	@Override
	public Page<CardShopDto> getCardShopListPage(Page page, CardShopDto cardShopDto) {
		Page<CardShopDto> iPage = vOrderCardShopMapper.getCardShopListPage(page, cardShopDto);
		log.info("->根据优惠方案ID给数据赋值优惠价格");
		List<CardShopDto> cardShopDtoList = iPage.getRecords();
		if (null != cardShopDtoList && cardShopDtoList.size() > 0){
			List<Long> discountPlanIdList = cardShopDtoList.stream().map(CardShopDto::getDiscountPlanId).collect(Collectors.toList());
			List<VDiscountPlan> discountPlanList = new VDiscountPlan().selectList(
					Wrappers.lambdaQuery(VDiscountPlan.class).in(VDiscountPlan::getId, discountPlanIdList));
			cardShopDtoList.stream().forEach(itme -> {
				discountPlanList.stream().forEach(plan ->{
					if (itme.getDiscountPlanId().equals(plan.getId())){
						itme.setDiscount(plan.getDiscount());
						itme.setLowMoney(plan.getLowMoney());
						itme.setPlanType(plan.getPlanType());
					}
				});
			});
			iPage.setRecords(cardShopDtoList);
		}
		return iPage;
	}

	/**
	 * 根据会员卡ID获取 卡还能使用的门店数量
	 * @param infoId  会员卡ID
	 * @param planType  方案类型  0企业优惠  1活动优惠  (不传为全部)
	 * @return
	 */
	@Override
	public R<Integer> getByInfoIdCountShop(Long infoId, Integer planType) {
		CardShopDto cardShopDto = new CardShopDto();
		cardShopDto.setPlanType(planType);
		log.info("->获取会员卡信息");
		VInfo vInfo = new VInfo().selectById(infoId);
		log.info("->获取卡信息");
		VCard card = new VCard().selectById(vInfo.getCardId());
		if (card.getDiscountType().equals(CardConstant.DISCOUNT_TYPE_QB)){
			log.info("->获取卡关联的群组ID");
			List<VCardGroup> cardGroupList = new VCardGroup().selectList(
					Wrappers.lambdaQuery(VCardGroup.class).eq(VCardGroup::getCardId, card.getId()));
			List<Long> groupIdList = cardGroupList.stream().map(VCardGroup::getGroupId).collect(Collectors.toList());
			log.info("->根据群组ID集合 获取门店ID集合");
			List<VGroupShop> shopIdList = new VGroupShop().selectList(
					Wrappers.lambdaQuery(VGroupShop.class).in(VGroupShop::getGroupId, groupIdList));
			cardShopDto.setShopIdList(shopIdList.stream().map(VGroupShop::getShopId).collect(Collectors.toList()));
		} else {
			List<VOrderCardShop> vOrderCardShops = new VOrderCardShop().selectList(Wrappers.lambdaQuery
					(VOrderCardShop.class).eq(VOrderCardShop::getOrderCardId, vInfo.getOrderCardId()));
			if (null != vOrderCardShops && vOrderCardShops.size() > 0){
				cardShopDto.setShopIdList(vOrderCardShops.stream().map(VOrderCardShop::getShopId).collect(Collectors.toList()));
			}
		}
		if (null == cardShopDto.getShopIdList() || cardShopDto.getShopIdList().size() == 0){
			return R.ok(0);
		}
		List<CardShopDto> cardShopDtoList = this.getCardShopPage(cardShopDto);
		return R.ok(cardShopDtoList.size());
	}
}
